package main

import (
	"fmt"
	//	"math/rand"
	"os"
	"bufio"
)

func main() {
	ReadFileByLine("C:\\Java\\jre7\\COPYRIGHT")
}

func ReadFileExample1() {
	fmt.Println("test File.Read() function")
	file, e := os.Open("/root/dev/test.go")
	defer file.Close()
	fmt.Println("read file", file, e)
	if e == nil {
		var buffer []byte = make([]byte, 16)
		//		var ee error
		//		var n int
		var (
			ee error
			n  int
		)
		for ee == nil {
			n, ee = file.Read(buffer)
			fmt.Print(string(buffer[0:n]))
		}
	}
}
func ReadFileExample2() {
	fmt.Println("test File.OpenFile() function")
	file, er := os.OpenFile("/root/dev/test.go", os.O_RDONLY, 0)
	defer file.Close()
	fmt.Println("read file", file, er)
	if er == nil {
		var buffer []byte = make([]byte, 16)
		var (
			n   int
			er1 error
		)
		for er1 == nil {
			n, er1 = file.Read(buffer)
			fmt.Print(string(buffer[0:n]))
		}
	}
}

func WriteFileExample() {
	var filePath string = "/root/tmp/tmptest.txt"
	file, er := os.Create(filePath)
	defer file.Close()
	if er != nil {
		fmt.Println("creating file error: ", er)
		return
	}
	file.WriteString("Hello world\n")
	file.Write([]byte("this is a test"))
	fmt.Println("file :", file.Name())
}

func FileExistExample() {
	var filePath string = "/root/tmp/t.shx"
	file, e := os.Open(filePath)
	if e != nil {
		fmt.Println("file read error", e)
		fmt.Printf("error type: %T\n", e)
	} else {
		fmt.Println("file exists...", file.Name())
	}
}

func CreateFileExample() {
	fmt.Println("test create file ...")
	// create file with nothing in it
	file, er := os.Create("/root/testcreatefile.txt")
	fmt.Println("create file result : ", file, er)
	if er == nil {
		fmt.Println("file path: ", file.Name())
	}
}

func DeleteFileExample() {
	fmt.Println("test delete file(remove)...")
	var filePath string = "/root/test"
	er := os.Remove(filePath)
	if er != nil {
		fmt.Println("delete error :", er)
	}
}

func DeleteDirExample() {
	fmt.Println("test delete directory")
	var dirPath = "/root/tmp/tf"
	er := os.RemoveAll(dirPath)
	if er != nil {
		fmt.Println("delete directory error:", er)
	}
}

func FileInfoExample() {
	printFileInfo("/root/tmp")
	fmt.Println("------------------------------------------")
	printFileInfo("/root/tmp/t.sh")
	fmt.Println("------------------------------------------")
	printFileInfo("/root/tmp/tl")
}

func printFileInfo(filePath string) {
	// Lstat(filePath_ or Stat(filePath)
	fi, e := os.Lstat(filePath)
	if e == nil {
		fmt.Println("file info Name:", fi.Name())
		fmt.Println("file info IsDir:", fi.IsDir())
		fmt.Println("file info Mode:", fi.Mode())
		fmt.Println("file info ModTime:", fi.ModTime())
		fmt.Println("file info Size:", fi.Size())
		fmt.Println("file info Sys:", fi.Sys())
	} else {
		fmt.Println("file info getting error", e)
	}
}

func ListSubFileItemsExample() {
	fmt.Println("test function of ls command on Linux or dir command on Windows")
	file, er := os.Open("/root")
	if er != nil {
		fmt.Println("error when getting file info...", er)
		return
	}
	names, er2 := file.Readdirnames(-1)
	if er2 != nil {
		fmt.Println("error when getting sub file info...", er2)
		return
	}
	for i, s := range names {
		fmt.Println("sub item :", i, ":", s)
	}
	fmt.Println("----------------------------------------------")
	// we should re-open the dir to get sub items again.
	file, er = os.Open("/root")
	if er != nil {
		fmt.Println("error when getting file info 2...", er)
		return
	}
	infos, er3 := file.Readdir(-1)
	if er3 != nil {
		fmt.Println("error when getting sub fileinfo info...", er3)
		return
	}
	fmt.Println("file info read: ", infos)
	for i, s := range infos {
		fmt.Println("sub item :", i, "--->", s.Name(), "  --- IsDir:", s.IsDir(), " --- Size:", s.Size())
	}
	fmt.Println("end of ListSubFilesExample")
}

func ReadFileByLine(filePath string){
	file,err:=os.Open(filePath)
	if err!=nil {
		fmt.Println("error from os.Open()")
	}
	defer file.Close()
	reader:=bufio.NewReader(file)
	count:=0
	for{
		linebytes,isPrefix,er:=reader.ReadLine()
		if er!=nil {
			break
		}
		fmt.Println("Line",count,"Prefix",isPrefix,"---->",string(linebytes))
		count++
	}
	
}